package sk.ownage.p1.featurestore;

import java.io.File;

import android.util.Log;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;

public class Track {
	private final String TAG="Track";
	private static final int EMPTY_TRACK=-1;
	private int id;
	private String name="",activity="",description="";
	private long timestamp=0;
	private String trackRootPath="";
	private TrackWriter trackWriter=null;
	private TrackReader trackReader=null;
	private TrackGeometry trackGeometry=null;
	private TrackMapOverlay trackMapOverlay=null;
	
	public Track(){
		this(EMPTY_TRACK);
	}

	public Track(int trackId){
		this(trackId,"");
	}
	
	public Track(int trackId, String trackRootPath){
		id=trackId;
		this.trackRootPath=trackRootPath;
		if(id==EMPTY_TRACK){
			try {
				trackWriter = new TrackWriter((!this.trackRootPath.equals(""))?this.trackRootPath+"/tracks":"");
				id=trackWriter.getActualTrackId();
			} catch (Exception e) {
				Log.d(TAG,e.toString());
			}
		}
	}
	
	public TrackWriter getTrackWriter(){
		return trackWriter;
	}
	
	public TrackReader getTrackReader(){
		if(id!=EMPTY_TRACK){
			try {
				trackReader=new TrackReader(id,(!this.trackRootPath.equals(""))?this.trackRootPath+"/tracks":"");
				name=trackReader.readName();
				activity=trackReader.readActivity();
				timestamp=trackReader.readTimestamp();
				description=trackReader.readDescription();
				trackReader=new TrackReader(id,(!this.trackRootPath.equals(""))?this.trackRootPath+"/tracks":"");
			} catch (Exception e) {
				Log.d(TAG,e.toString());
			}
		}
		return trackReader;
	}
	
	public void addWaypointToTrackGeometry(LatLng position,long timestamp,boolean newSegment){
		if(id!=EMPTY_TRACK){
			if(trackGeometry==null)
				trackGeometry=new TrackGeometry(id,(!this.trackRootPath.equals(""))?this.trackRootPath+"/geometry":"");
			trackGeometry.addWaypoint(position, timestamp, newSegment);
		}
	}
	
	
	public void createGeometry(){
		if(id!=EMPTY_TRACK)
			if(trackGeometry==null)
				trackGeometry=new TrackGeometry(id,(!this.trackRootPath.equals(""))?this.trackRootPath+"/geometry":"");
	}
	
	public boolean hasGeometry(){
		int folderId = id / TrackQuadTreeRoot.DIRECTORY_MAX;
		int fileId = id % TrackQuadTreeRoot.DIRECTORY_MAX;;
		String folderPath = (((folderId / 100) > 0) ? "" : "0")
				+ (((folderId / 10) > 0) ? "" : "0") + folderId;
		String filePath = (((fileId / 100) > 0) ? "" : "0")
				+ (((fileId / 10) > 0) ? "" : "0") + fileId;
		File geometryFile=new File(((!this.trackRootPath.equals(""))?this.trackRootPath+"/geometry" :TrackQuadTreeRoot.rootPath) + "/" + folderPath + "/" + filePath + "/4/size.bin");
		return geometryFile.exists();
	}
	
	public void draw(GoogleMap map, int color){
		if(id!=EMPTY_TRACK){
			if(trackMapOverlay==null)
				trackMapOverlay=new TrackMapOverlay(id,(!this.trackRootPath.equals(""))?this.trackRootPath+"/geometry":"");
			trackMapOverlay.drawTrack(map,color);
		}
	}
	
	public void clearMap(){
		if(id!=EMPTY_TRACK){
			if(trackMapOverlay!=null)
				trackMapOverlay.cleanMap();
		}
	}
	
	public String getTrackName(){
		if(id!=EMPTY_TRACK){
			if(trackReader==null)
				trackReader=getTrackReader();
		}
		return name;
	}
	
	public String getTrackActivity(){
		if(id!=EMPTY_TRACK){
			if(trackReader==null)
				trackReader=getTrackReader();
		}
		return activity;
	}
	
	public long getTrackTimestamp(){
		if(id!=EMPTY_TRACK){
			if(trackReader==null)
				trackReader=getTrackReader();
		}
		return timestamp;
	}
	
	public String getTrackDescription(){
		if(id!=EMPTY_TRACK){
			if(trackReader==null)
				trackReader=getTrackReader();
		}
		return description;
	}
	
	public int getID(){
		return id;
	}

}

